package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Dept;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;
import com.by.common.group.IUpdate;
import com.by.query.DeptQuery;
import com.by.service.DeptService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1")
@Api(tags = "部门管理")
public class DeptController extends BaseController {

    @Autowired
    DeptService deptService;

    @ApiOperation("保存部门")
    @PostMapping("/dept")
    public ResultBean save(@Validated(ISave.class) @RequestBody Dept dept, BindingResult result) {
        validate(result);
        int rs = deptService.save(dept);
        return saveResult(rs);
    }


    @ApiOperation("修改部门")
    @PutMapping("/dept")
    public ResultBean update(@Validated(IUpdate.class) @RequestBody Dept dept, BindingResult result) {
        validate(result);
        int rs = deptService.update(dept);
        return saveResult(rs);
    }

    @ApiImplicitParam(name = "id", value = "部门id", dataTypeClass = Integer.class, required = true, paramType = "query",example = "1")
    @ApiOperation("删除部门")
    @DeleteMapping("/dept")
    public ResultBean delete(@RequestParam(name = "id", required = true, defaultValue = "0") int id) {
        int rs = deptService.delete(id);
        if (rs == 2) {
            return fail("请先删除子部门");
        }
        return deleteResult(rs);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门id", dataTypeClass = Integer.class, required = true, paramType = "query",example = "1"),
            @ApiImplicitParam(name = "status", value = "部门状态0禁用1启用", dataTypeClass = Integer.class, required = true, paramType = "query",example = "1")}

    )
    @ApiOperation("修改部门状态")
    @PutMapping("/dept/status")
    public ResultBean updateStatus(@RequestParam(name = "id", required = true) int id, @RequestParam(name = "status",required = true, defaultValue = "0") int status) {
        int rs = deptService.updateStatus(id, status);
        return updateResult(rs);
    }

    @ApiOperation("查询部门")
    @PostMapping("/dept/query")
    public ResultBean query(@RequestBody DeptQuery deptQuery){
        List list = deptService.queryChildDept(deptQuery);
        return success("部门列表",list);
    }

    /**
     * 查询部门树
     *
     * @return
     */
    @ApiOperation("查询部门树")
    @GetMapping("/dept/tree")
    public ResultBean queryTree() {
        List tree = deptService.queryTree();
        return success("部门树", tree);
    }

}
